<template>
  <view class="my-settlt-container">
    <!-- 全选区域 -->
    <label class="settlt-left" @click="changeChecked">
      <radio :checked="isFullChecked" color="#C00000"></radio>
    </label>
    <!-- 合计区域 -->
    <view class="sum-box">
     合计： <text class="sum">￥{{checkedPrice}}</text>
    </view>
    <!-- 结算按钮 -->
    <view class="btn-settle" @click="btnSettle">
      结算({{checkedCount}})
    </view>
  </view>
</template>

<script>
  import {mapGetters,mapMutations,mapState} from 'vuex'
  export default {
    name:"my-settle",
    data() {
      return {
        second:3,
        timer:null
      };
    },
    computed:{
      ...mapGetters('m_cart',['checkedCount','checkedPrice','total']),
      ...mapGetters('m_user',['addstr']),
      ...mapState('m_user',['address','token']),
      ...mapState('m_cart',['cart']),
      // 全选框选中状态
      isFullChecked(){
        return this.total === this.checkedCount
      }
    },
    methods:{
      ...mapMutations('m_cart',['updateAllGoodsState']),
      ...mapMutations('m_user',['updateRedirectInfo']),
      changeChecked(){
        this.updateAllGoodsState(!this.isFullChecked)
      },
      btnSettle(){
        if(!this.checkedCount) return uni.showMsg('你还没有选中任何商品',1500)
        if(!this.addstr) return uni.showMsg('请选择收货地址',1500)
        if(!this.token) return this.delayNavigate()
        this.payOrder()
      },
      showTips(n){
        uni.showToast({
          icon:'none',
          title:'请登录后在结算' + n +'秒后自动跳转到登录页面',
          mask:true,
          duration:1500
        })
      },
      delayNavigate(){
        this.second = 3
        this.showTips(this.second)
        this.timer = setInterval(()=>{
          this.second--
          if(this.second<=0){
            clearInterval(this.timer)
            uni.switchTab({
              url:'/pages/my/my',
              success:() =>{
                this.updateRedirectInfo({
                  openType:'switchTab',
                  url:'/pages/cart/cart'
                })
              }
            })
            return
          }
          
          this.showTips(this.second)
        },1000)
      },
      //商品支付
     async payOrder(){
        const orderInfo ={
          order_price:0.01,
          consignee_addr:this.addstr,
          goods:this.cart.filter(x => x.goods_state).map(item => ({goods_id:item.goods_id, goods_number:item.goods_count, goods_price:item.goods_price}))
        }
        //发请求创建订单
        const {data:res} = await uni.$http.post('/orders/create',orderInfo)
        console.log(res);
        // if (res.meta.status !== 200) return uni.showMsg('创建订单失败！',1500)
        const order_number = 'JBNJKBDHJLNB'
        const {data:res2} = await uni.$http.post('/orders/req_unifiedorder',{order_number:order_number})
        console.log(res2);
        const payInfo ={
          "timeStamp":"1525681145",
          "nonceStr":"BkPggorBXZwPGXe3",
          "package":"prepay_id=wx071619042918087bb4c1d3d72999385683",
          "signType":"MD5",
          "paySign":"D1642DEEF1663C8012EDEB9297E1D516"
        }
        const [err,succ] = await uni.requestPayment(payInfo)
        console.log();
      }
    }
  }
</script>

<style lang="scss">
.my-settlt-container{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px;
  .settlt-left {
    display: flex;
    align-items: center;
  }
  .sum {
    color: #C00000;
  }
  .btn-settle {
    height: 50px;
    min-width: 100px;
    background-color: #C00000;
    color: white;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
  }
}
</style>